﻿#region Header

/**
 * <pre>
 *
 *  Work Force Management
 *  File: LanguageReader.cs
 *
 *  Grandsys, Inc.
 *  Copyright (C): 2009
 *
 *  Description:
 *  Init LanguageReader
 *
 *  Note
 *  Created By: Prime Li at 7/15/2009 2:10:04 PM
 *
 * </pre>
 */

#endregion Header

namespace Luna.Globalization
{
    using System;
    using System.Globalization;
    using System.IO;
    using System.Windows;
    using System.Windows.Markup;

    /// <summary>
    /// Read the Language
    /// </summary>
    public static class LanguageReader
    {
        #region Fields

        private const string LANGUAGE_PATH = @".\Languages\";
        private static ResourceDictionary _currentLanguage;
        private static ResourceDictionary _defaultLanguage;

        #endregion Fields

        static LanguageReader()
        {
            Uri uri = new Uri("XWord.Globalization;component/Languages/default.xaml", UriKind.RelativeOrAbsolute);
            _defaultLanguage = Application.LoadComponent(uri) as ResourceDictionary;
            _currentLanguage = _defaultLanguage;
        }

        #region Methods

        /// <summary>
        /// Loads the specified culture.
        /// </summary>
        /// <param name="culture">The culture.</param>
        public static void Load(string culture)
        {
            bool isExistLanguage = CheckLanguageName(culture);
            if (isExistLanguage)
            {
                string path = LANGUAGE_PATH + culture + ".xaml";
                using (FileStream fs = File.OpenRead(path))
                {
                    _currentLanguage = XamlReader.Load(fs) as ResourceDictionary;
                    //屏蔽此段代码，这样节省Applicaion资源字典的内存使用，加快查找速度
                    //缺点是无法使用StaticResource/DynamicResource,只能使用inf:Resource
                    //if (Application.Current.Resources.MergedDictionaries.Contains(_currentLanguage))
                    //{
                    //    Application.Current.Resources.MergedDictionaries.Remove(_currentLanguage);
                    //}

                    //Application.Current.Resources.MergedDictionaries.Add(_currentLanguage);
                }

                Application.Current.Dispatcher.Thread.CurrentCulture = CultureInfo.GetCultureInfo(culture);
                Application.Current.Dispatcher.Thread.CurrentUICulture = CultureInfo.GetCultureInfo(culture);
            }
            else
            {
                //Use default Language
                Application.Current.Dispatcher.Thread.CurrentCulture = CultureInfo.GetCultureInfo("zh-CN");
                Application.Current.Dispatcher.Thread.CurrentUICulture = CultureInfo.GetCultureInfo("zh-CN");
            }
        }

        public static void LoadResource(string fileName)
        {
            if (File.Exists(fileName))
            {
                using (FileStream fs = File.OpenRead(fileName))
                {
                    ResourceDictionary dic = XamlReader.Load(fs) as ResourceDictionary;
                    //如果不是WPF程序
                    if (Application.Current == null)
                        _currentLanguage = dic;
                    else
                        Application.Current.Resources.MergedDictionaries.Add(dic);
                }
            }
        }

        /// <summary>
        /// Gets the value.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public static string GetValue(string key)
        {
            object value = GetValue((object)key);
            if (value == null)
                return string.Empty;
            return value.ToString();
        }

        /// <summary>
        /// Gets the value.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public static object GetValue(object key)
        {
            if (key == null)
                return null;
            object value = _currentLanguage[key];
            if (value == null)
                value = _defaultLanguage[key];
            return value;
        }

        /// <summary>
        /// Gets the value.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public static T GetValue<T>(object key)
        {
            object value = GetValue(key);
            if (value == null)
                return default(T);
            return (T)value;
        }

        public static bool ContainsKey(string key)
        {
            return ContainsKey((object)key);
        }

        public static bool ContainsKey(object key)
        {
            return _currentLanguage.Contains(key) || _defaultLanguage.Contains(key);
        }

        /// <summary>
        /// Checks the name of the language.
        /// </summary>
        /// <param name="languageName">Name of the language.</param>
        /// <returns></returns>
        private static bool CheckLanguageName(string languageName)
        {
            var allLanguageNames = Directory.GetFiles(LANGUAGE_PATH);
            foreach (var name in allLanguageNames)
            {
                if (name.Contains(languageName))
                {
                    return true;
                }
            }

            return false;
        }

        #endregion Methods
    }
}
